home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / octa209s.zip / octave-2.09 / liboctave / MArray2.cc < prev    next >
C/C++ Source or Header  |  1996-10-12  |  4KB  |  175 lines

  1. /*
  2.  
  3. Copyright (C) 1996 John W. Eaton
  4.  
  5. This file is part of Octave.
  6.  
  7. Octave is free software; you can redistribute it and/or modify it
  8. under the terms of the GNU General Public License as published by the
  9. Free Software Foundation; either version 2, or (at your option) any
  10. later version.
  11.  
  12. Octave is distributed in the hope that it will be useful, but WITHOUT
  13. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  15. for more details.
  16.  
  17. You should have received a copy of the GNU General Public License
  18. along with Octave; see the file COPYING.  If not, write to the Free
  19. Software Foundation, 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
  20.  
  21. */
  22.  
  23. #if defined (__GNUG__)
  24. #pragma implementation
  25. #endif
  26.  
  27. #ifdef HAVE_CONFIG_H
  28. #include <config.h>
  29. #endif
  30.  
  31. #include "MArray2.h"
  32. #include "lo-error.h"
  33.  
  34. #include "MArray-defs.h"
  35.  
  36. // Two dimensional array with math ops.
  37.  
  38. // Element by element MArray2 by scalar ops.
  39.  
  40. template <class T>
  41. MArray2<T>&
  42. operator += (MArray2<T>& a, const T& s)
  43. {
  44.   DO_VS_OP2 (+=)
  45.   return a;
  46. }
  47.  
  48. template <class T>
  49. MArray2<T>&
  50. operator -= (MArray2<T>& a, const T& s)
  51. {
  52.   DO_VS_OP2 (-=)
  53.   return a;
  54. }
  55.  
  56. // Element by element MArray2 by MArray2 ops.
  57.  
  58. template <class T>
  59. MArray2<T>&
  60. operator += (MArray2<T>& a, const MArray2<T>& b)
  61. {
  62.   int r = a.rows ();
  63.   int c = a.cols ();
  64.   int br = b.rows ();
  65.   int bc = b.cols ();
  66.   if (r != br || c != bc)
  67.     gripe_nonconformant ("operator +=", r, c, br, bc);
  68.   else
  69.     {
  70.       if (r > 0 && c > 0)
  71.     {
  72.       int l = a.length ();
  73.       DO_VV_OP2 (+=);
  74.     }
  75.     }
  76.   return a;
  77. }
  78.  
  79. template <class T>
  80. MArray2<T>&
  81. operator -= (MArray2<T>& a, const MArray2<T>& b)
  82. {
  83.   int r = a.rows ();
  84.   int c = a.cols ();
  85.   int br = b.rows ();
  86.   int bc = b.cols ();
  87.   if (r != br || c != bc)
  88.     gripe_nonconformant ("operator -=", r, c, br, bc);
  89.   else
  90.     {
  91.       if (r > 0 && c > 0)
  92.     {
  93.       int l = a.length ();
  94.       DO_VV_OP2 (-=);
  95.     }
  96.     }
  97.   return a;
  98. }
  99.  
  100. // Element by element MArray2 by scalar ops.
  101.  
  102. #define MARRAY_A2S_OP(OP) \
  103.   template <class T> \
  104.   MArray2<T> \
  105.   operator OP (const MArray2<T>& a, const T& s) \
  106.   { \
  107.     DO_VS_OP (OP); \
  108.     return MArray2<T> (result, a.rows (), a.cols ()); \
  109.   }
  110.  
  111. MARRAY_A2S_OP (+)
  112. MARRAY_A2S_OP (-)
  113. MARRAY_A2S_OP (*)
  114. MARRAY_A2S_OP (/)
  115.  
  116. // Element by element scalar by MArray2 ops.
  117.  
  118. #define MARRAY_SA2_OP(OP) \
  119.   template <class T> \
  120.   MArray2<T> \
  121.   operator OP (const T& s, const MArray2<T>& a) \
  122.   { \
  123.     DO_SV_OP (OP); \
  124.     return MArray2<T> (result, a.rows (), a.cols ()); \
  125.   }
  126.  
  127. MARRAY_SA2_OP (+)
  128. MARRAY_SA2_OP (-)
  129. MARRAY_SA2_OP (*)
  130. MARRAY_SA2_OP (/)
  131.  
  132. // Element by element MArray2 by MArray2 ops.
  133.  
  134. #define MARRAY_A2A2_OP(FCN, OP) \
  135.   template <class T> \
  136.   MArray2<T> \
  137.   FCN (const MArray2<T>& a, const MArray2<T>& b) \
  138.   { \
  139.     int r = a.rows (); \
  140.     int c = a.cols (); \
  141.     int br = b.rows (); \
  142.     int bc = b.cols (); \
  143.     if (r != br || c != bc) \
  144.       { \
  145.         gripe_nonconformant (#FCN, r, c, br, bc); \
  146.     return MArray2<T> (); \
  147.       } \
  148.     if (r == 0 || c == 0) \
  149.       return MArray2<T> (); \
  150.     int l = a.length (); \
  151.     DO_VV_OP (OP); \
  152.     return MArray2<T> (result, r, c); \
  153.   }
  154.  
  155. MARRAY_A2A2_OP (operator +, +)
  156. MARRAY_A2A2_OP (operator -, -)
  157. MARRAY_A2A2_OP (product,    *)
  158. MARRAY_A2A2_OP (quotient,   /)
  159.  
  160. // Unary MArray2 ops.
  161.  
  162. template <class T>
  163. MArray2<T>
  164. operator - (const MArray2<T>& a)
  165. {
  166.   NEG_V;
  167.   return MArray2<T> (result, a.rows (), a.cols ());
  168. }
  169.  
  170. /*
  171. ;;; Local Variables: ***
  172. ;;; mode: C++ ***
  173. ;;; End: ***
  174. */
  175.